﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TestingIIS.Models;
using Oracle.DataAccess.Client;
using Oracle.Web;


namespace TestingIIS.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        private Entities _entity = new Entities();
        public ActionResult Index()
        {
            ViewData["Message"] = _entity.Connection.ConnectionString;
            return View(_entity.CLIENTs.ToList());
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult ViewClient()
        {
            return View();
        }
        public ActionResult Search(string email)
        {
            var result = _entity.CLIENTs.Where(a => a.EMAIL.Contains(email));
            return View(result.ToList());
        }

        public ActionResult Details()
        {
            return View();
        }

        public ActionResult DetailView()
        {
            
            CLIENT client = _entity.CLIENTs.Where(a => a.EMAIL.Contains("abc")).First();
            OracleConnection myConnection = new OracleConnection();
            OracleCommand myCommand = myConnection.CreateCommand();
            OracleTransaction myTransaction;
            string commandString1 = "insert into client(email,pwd) values('roy.hpr1@gmail.com','123456')";
            string commandString2 = "insert into client(email,pwd) values('roy.hpr1@gmail.com','123456')";
            myConnection.ConnectionString = "Data Source=sid3.comp.nus.edu.sg/sid3.comp.nus.edu.sg;User Id=A0087753;Password=cs2102std";
            myConnection.Open();
            //myCommand.Connection = myConnection;
            myCommand.CommandType = System.Data.CommandType.Text;
            myCommand.CommandText = commandString1;

            myTransaction = myConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
            myCommand.Transaction = myTransaction;

            try
            {
                
                if (myConnection.State == System.Data.ConnectionState.Open)
                {

                    myCommand.ExecuteNonQuery();

                    myCommand.CommandText = commandString2;
                    myCommand.ExecuteNonQuery();

                    myTransaction.Commit();

                    myConnection.Close();
                }
            }
            catch
            {
                myTransaction.Rollback();
                if (myConnection.State == System.Data.ConnectionState.Open)
                {
                    myConnection.Close();
                }
            }
            return View(client);
        }


        public ActionResult Delete(string email)
        {
            //ViewData["Message"] = email;
            try
            {
                CLIENT client = _entity.CLIENTs.Where(a => a.EMAIL.Contains(email)).First();
                _entity.CLIENTs.DeleteObject(client);
                _entity.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
               
            }
            return View();
            
        }
    }
}
